
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.InputMismatchException;
import java.util.Scanner;


public class FriendController {
	
	private FriendManagement friendManagement;
	private Layout layout;
	
	public FriendController(){
		this(null,null);
	}
	
	public FriendController(FriendManagement friendManagement, Layout layout){
		this.friendManagement = friendManagement;
		this.layout = layout;
	}
	
	public void menu(){
		Scanner sc = new Scanner(System.in);
		String choice = null;
		System.out.println("All Friends:");
		layout.showFriend(friendManagement.getAllFriends());
		while(true){
			layout.showCommand();
			choice = sc.nextLine();
			
			switch(choice){
				case "1":
				case "A":
				case "a":
					int i=0;
					Friend friend = new Friend();
					System.out.println("Enter new friend: ");
					System.out.print("Enter new name:");
					friend.setName(sc.nextLine());
					System.out.print("Enter new gender: ");
					friend.setGender(sc.nextLine());
					System.out.print("Enter new address: ");
					friend.setAddress(sc.nextLine());
					if(friendManagement.insert(friend)){
						System.out.println("You have been inserted successfully!!! "+ i);
					}else{
						System.out.println("You have not been inserted!!!");
					}		
					break;
				
				case "2":
				case "D":
				case "d":
					int deleteId = 0;
					while(true){
						try{
							System.out.print("Enter friend's id you want to delete: ");
							deleteId = sc.nextInt();
							break;
						}catch(InputMismatchException ex){
							System.out.println("You can enter only the number!!!");
							sc.nextLine();
							continue;
						}
					}
					if(friendManagement.delete(deleteId)){
						System.out.println("You have been deleted successfully!!!");
					}else{
						System.out.println("No that friend's id!!!");
					}
					sc.nextLine();
					break;
				
				case "3":
					int updateId = 0;
					while(true){
						try{
							System.out.println("Enter friend's id you want to update: ");
							updateId = sc.nextInt();
							break;
						}catch(InputMismatchException ex){
							System.out.println("You can enter only the number!!!");
							sc.nextLine();
							continue;
						}
					}
					Friend updateFriend = friendManagement.getFriend(updateId);
					if(updateFriend!=null){
						System.out.print("Enter new friend's name: ");
						sc.nextLine();
						updateFriend.setName(sc.nextLine());
						System.out.print("Enter new friend's gender: ");
						updateFriend.setGender(sc.nextLine());
						System.out.print("Enter new friend's addrss: ");
						updateFriend.setAddress(sc.nextLine());
						if(friendManagement.update(updateFriend)){
							System.out.println("You have been updated successfully!!!");
						}else{
							System.out.println("Cannot update!!!");
						}
					}
					else{
						System.out.println("Not found that friend's id!!!");
					}					
					break;
				case "4":
					System.out.println("Enter friend's name you want to search?");
					String friendName = sc.nextLine();
					ResultSet rs = friendManagement.searchByName(friendName);
					try {
						if(rs.next()){
							layout.showFriend(rs);
//							rs.absolute(1);
//							while(rs.next()){
//								rs.updateString(2, "TOLA");
//								rs.updateRow();
//							}
						}else{
							System.out.println("Search not found!!!");
						}
					} catch (SQLException ex) {
						System.err.println(ex.getMessage());
					}
					break;
				case "5":
					System.out.println("All Friends:");
					layout.showFriend(friendManagement.getAllFriends());
					break;
				case "6":
					System.out.println("Good bye!!!");
					ConnectionManagement.closeConnection();
					System.exit(0);
					break;
				default:
					System.out.println("Please try again!!!");
			}
		}
	}
	
	
	
}
